package com.asialjim.wechat.official.apiservice.impl;

import com.asialjim.wechat.apiservice.constant.WeChatConstants;
import com.asialjim.wechat.core.api.WeChatApiDefaultRes;
import com.asialjim.wechat.core.executor.WeChatApiExecutor;
import com.asialjim.wechat.official.api.OfficialApi;
import com.asialjim.wechat.official.api.templatemessage.*;
import com.asialjim.wechat.official.apiservice.TemplateMessageService;
import com.asialjim.wechat.official.templatemessage.TemplateMessage;
import com.asialjim.wechat.pojo.account.WeChatAccountHolder;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.List;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: TemplateMessageServiceImpl ]</i></h2><strong>
 * <p> template message service implement
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/15   &nbsp;&nbsp; JDK 8
 */
@Data
@Slf4j
@AllArgsConstructor
public class TemplateMessageServiceImpl implements TemplateMessageService {
    private WeChatApiExecutor executor;

    @Override
    public void apiSetIndustry(SetIndustryApiRequest.BodyParam industryInfo) {
        log.info("\r\n\tset api industry:[{}]",industryInfo);
        WeChatApiDefaultRes response = executor.execute(OfficialApi.API_SET_INDUSTRY, industryInfo, WeChatApiDefaultRes.class).parse();
        log.info("\r\n\tset api industry result:[{}]",response);
    }

    @Override
    public GetIndustryApiResponse industryInfo() {
        GetIndustryApiResponse response = executor.execute(OfficialApi.GET_API_INDUSTRY, GetIndustryApiResponse.class);
        log.info("\r\n\tget industry response:[{}]",response);
        return response;
    }

    @Override
    public String addTemplate(String templateShortId) {
        AddTemplateApiRequest.BodyParam bodyParam = AddTemplateApiRequest.BodyParam.builder().template_id_short(templateShortId).build();
        AddTemplateApiResponse response = executor.execute(OfficialApi.ADD_TEMPLATE, bodyParam, AddTemplateApiResponse.class).parse();
        log.info("\r\n\tadd template api response result:[{}]", response);
        return response.getTemplate_id();
    }

    @Override
    public List<GetAllPrivateTemplateApiResponse.TemplateInfo> allPrivateTemplate() {
        GetAllPrivateTemplateApiResponse response = executor.execute(OfficialApi.GET_ALL_PRIVATE_TEMPLATE, GetAllPrivateTemplateApiResponse.class).parse();
        log.info("\r\n\tget all private message template[{}]",response);
        return response.getTemplate_list();
    }

    @Override
    public void deletePrivateTemplate(String templateId) {
        DeletePrivateTemplateApiRequest.BodyParam bodyParam = DeletePrivateTemplateApiRequest.BodyParam.builder().template_id(templateId).build();
        WeChatApiDefaultRes response = executor.execute(OfficialApi.DELETE_PRIVATE_TEMPLATE, bodyParam, WeChatApiDefaultRes.class);
        log.info("\r\n\tdelete private message template:[{}]",response);
    }

    @Override
    public String onceSubscribeTemplateUrl(String scene, String templateId, String redirectUrl, String reserved) {
        String appId = WeChatAccountHolder.get().getAppId();
        String url = String.format(WeChatConstants.WeChatApiUrl.GET_SUBSCRIBE_MSG, appId, scene, templateId, redirectUrl, reserved);
        log.info("\r\n\tget once subscribe template url result:[{}]",url);
        return url;
    }

    @Override
    public void sendSubscribeTemplateMessage(TemplateMessage templateMessage) {
        log.info("\r\n\tsend subscribe template message:[{}]",templateMessage);
        SendTemplateApiRequest.BodyParam bodyParam = new SendTemplateApiRequest.BodyParam(templateMessage);
        WeChatApiDefaultRes response = executor.execute(OfficialApi.SEND_SUBSCRIBE_TEMPLATE_MESSAGE, bodyParam, WeChatApiDefaultRes.class);
        log.info("\r\n\tsend subscribe template message result:[{}]",response);
    }
}
